package com.rongji.dfish.demo.editor.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * <p>Title: 榕基I-TASK执行先锋</p>
 *
 * <p>Description: 专门为提高企业执行力而设计的产品</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: 榕基软件开发有限公司</p>
 * TreeDragGenerator 是用来生成带生成带树状修饰符的下拉列表的工具
 * @author I-TASK成员: LinLW
 * @version 1.0
 */
public class TreeDragGenerator {

  private char blank;
  private char i;
  private char t;
  private char l;
  private Map<Object,List<Object[]>> dataMap;
  private List<Object[]> result;
  private Collection<Object[]> dataList;


  private TreeDragGenerator(Collection<Object[]> dataList) {
    this.dataList = dataList;
  }

  /**
   * 传入参数,生成带树状修饰符的列表
   * @param dataList Collection<Object[]>
   * object[0]=value object[1]=label object[2]=pvalue
   * @return List<Object[]>
   */
  public static List generate(Collection<Object[]> dataList) {
    return generate(dataList, (char)12288, (char)9474, (char)9500, (char)9492);
  }

  /**
   * 传入参数,生成带树状修饰符的列表
   * @param dataList Collection<Object[]>
   * object[0]=value object[1]=label object[2]=pvalue
   * @param blank char 定义表示空格的树状修饰符
   * @param i char 定义表示竖线的树状修饰符
   * @param t char 定义表示三通的树状修饰符
   * @param l char 定义表示折线的树状修饰符
   * @return List
   */
  public static List generate(
      Collection<Object[]> dataList, char blank, char i, char t, char l) {
    TreeDragGenerator k = new TreeDragGenerator(dataList);
    k.blank = blank;
    k.i = i;
    k.t = t;
    k.l = l;
    return k.generate();
  }

  private List generate() {
    result = new ArrayList<Object[]>();
    dataMap = new HashMap<Object,List<Object[]>>();
    List<Object> pids = new ArrayList<Object>();
    Set<Object> ids = new HashSet<Object>();
    for (Object[] item: dataList) {
      ids.add(item[0]);
      List<Object[]> list;
      Object o = (item[2] == null) ? "" : item[2];
      if ( (list = dataMap.get(o)) == null) {
        dataMap.put(o, list = new ArrayList<Object[]>());
        pids.add(o);
      }
      list.add(item);
    }
    for (Object item2 : pids) {
      if (ids.contains(item2)) {
        continue;
      }
      for (Object[] item :dataMap.get(item2)) {
        work("", item);
      }
    }
    return result;
  }

  private void work(String prefix, Object[] content) {
    result.add(new Object[] {content[0], prefix + content[1]});
    List<Object[]> sonList = dataMap.get(content[0]);
    int sonLen = (sonList == null) ? 0 : sonList.size();
    if (sonLen > 0) {
      String newPrefix = "";
      if (prefix.length() > 1) {
        newPrefix = prefix.substring(0, prefix.length() - 1);
      }
      if (prefix.length() > 0) {
        char lastChar = prefix.charAt(prefix.length() - 1);
        if (lastChar == l ) {
          newPrefix += blank;
        }else if (lastChar == t) {
          newPrefix += i;
        }
      }
      int inte = 1;
      for (Iterator<Object[]> iter = sonList.iterator(); iter.hasNext(); inte++) {
        Object[] item = iter.next();
        if (inte < sonLen) {
          work(newPrefix + t, item);
        }
        else {
          work(newPrefix + l, item);
        }
      }
    }
  }
}
